﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using WrapperTool;

namespace WrapperToolsTester
{
    class Program
    {
        private static TypeHelper typeHelper;
        private const string const_wrapper = "Wrapper";
        private static string className ;
        private static string wrOwnerName;
        private static string baseTypeName;
        private static string wrClassName;

        static void Main(string[] args)
        {
            var type = typeof (A);
            
            typeHelper= new TypeHelper();
            var dicUsing = typeHelper.DicUsing;
            dicUsing.Add("System", "using System;");
            dicUsing.Add("System.Collections.Generic", "using System.Collections.Generic;");
            //dicUsing.Add("System.Data", "using System.Data;");
            dicUsing.Add("System.Text", "using System.Text;");
            dicUsing.Add("GrapeCity.Windows.Testing.Tools", "using GrapeCity.Windows.Testing.Tools;");
            if (!dicUsing.ContainsKey(type.Namespace))
            {
                dicUsing.Add(type.Namespace, "");
            }

            className = typeHelper.GetName(type);
            wrClassName = typeHelper.GetWrapperName(type);

           baseTypeName = typeHelper.GetDynamicWrapperName(type.BaseType);
            //如果BaseType是Abstract或者有Abstract成员,则忽略继承关系
            //不忽略抽象类了 2011/06/16
            //if (baseTypeName.Equals("Object") || type.BaseType.IsAbstract)
            if (baseTypeName.Equals("Object"))
            {
                baseTypeName = "";
            }
            StringBuilder stream = new StringBuilder();
            stream.AppendLine("namespace " + type.Namespace);
            stream.AppendLine("{");
            //抽象类 不 添加abstract关键字
            string publicClassStr = "    public class ";
            //if (type.IsAbstract)
            //{
            //    publicClassStr = "    public abstract class ";
            //}
            //获取泛型类的约束条件
            //数组转换支持
            string iWrapper = "IWrapper";
            string classConstraints = typeHelper.GetClassConstraint(type);
            if (baseTypeName == "")
            {
                stream.AppendLine(publicClassStr + wrClassName + " : " + iWrapper + classConstraints);
            }
            else
            {
                stream.AppendLine(publicClassStr + wrClassName + " : " + baseTypeName + ", " + iWrapper + classConstraints);
            }
            stream.AppendLine("    {");
            GetPrivateWrapper(type, stream);
            GetConstructorWrapper(type, stream);
            GetFieldsWrapper(type, stream);
            GetAllPropertiesWrapper(type, stream);
            GetMethodsWrapper(type, stream);
            //this.GetEventsWrapper(type, stream);
            GetIWrapperMembers(type, stream);
            stream.AppendLine("    }");
            stream.AppendLine("}");

            StringBuilder usingStr = new StringBuilder();
            dicUsing.Remove(type.Namespace);
            foreach (var key in dicUsing.Keys)
            {
                usingStr.AppendLine(dicUsing[key]);
            }
            usingStr.AppendLine("");

            Console.WriteLine(   usingStr.ToString() + stream.ToString());
           

        }

        private static void GetPrivateWrapper(Type type, StringBuilder stream)
        {
            wrOwnerName = typeHelper.GetFristName(type);
            if (wrOwnerName.Length > 1)
            {
                wrOwnerName = "_" + wrOwnerName[0].ToString().ToLower() + wrOwnerName.Substring(1);
            }
            else if (wrOwnerName.Length == 1)
            {
                wrOwnerName = "_" + wrOwnerName[0].ToString().ToLower();
            }

            stream.AppendLine("        #region Private");
            stream.AppendLine("");
            stream.AppendLine("        private " + className + " " + wrOwnerName + " = null;");
            stream.AppendLine("");
            stream.AppendLine("        #endregion");
            stream.AppendLine("");
        }

        private static void GetConstructorWrapper(Type type, StringBuilder stream)
        {
            string classConstructorName = typeHelper.GetFristName(type) + const_wrapper;
            string wrOwnerNamePar = wrOwnerName.Replace("_", "");
            stream.AppendLine("        #region Consturctor");
            stream.AppendLine("");
            stream.AppendLine("        public " + classConstructorName + "(" + className + " " + wrOwnerNamePar + ")");
            if (baseTypeName.EndsWithWrapper(const_wrapper))
            {
                stream.AppendLine("            : base(" + wrOwnerNamePar + ")");
            }
            stream.AppendLine("        {");
            //stream.AppendLine("            if (" + wrOwnerNamePar + " == null)");
            //stream.AppendLine("            {");
            //stream.AppendLine("                throw new ArgumentNullException(\"" + wrOwnerNamePar + " is null.\");");
            //stream.AppendLine("            }");
            //stream.AppendLine("");

            stream.AppendLine("                UISync.Current.InvokeSync(delegate");
            stream.AppendLine("                {");
            stream.AppendLine("                    this." + wrOwnerName + " = " + wrOwnerNamePar + ";");
            stream.AppendLine("                });");
            stream.AppendLine("        }");
            stream.AppendLine("");

            //构造器处理 start
            var constructors = type.GetConstructors();
            foreach (var constructor in constructors)
            {
                if (type.IsAbstract)
                {
                    break;
                }
                var parameters = constructor.GetParameters();
                //if (parameters.Length <= 0)
                //{
                //    //是默认构造器,跳过.
                //    continue;
                //}

                var dicParaStartStr = new Dictionary<int, string>();
                var parametersStr = GetParametersString(parameters, dicParaStartStr);
                stream.AppendLine("        public " + classConstructorName + "(" + parametersStr + ")");
                //if (baseTypeName.EndsWithWrapper(const_wrapper))
                //{
                //    stream.AppendLine("            : base(" + wrOwnerNamePar + ")");
                //}
                //stream.AppendLine("        {");
                //stream.AppendLine("            if (" + wrOwnerNamePar + " == null)");
                //stream.AppendLine("            {");
                //stream.AppendLine("                throw new ArgumentNullException(\"" + wrOwnerNamePar + " is null.\");");
                //stream.AppendLine("            }");
                //stream.AppendLine("");
                StringBuilder constCallSite = new StringBuilder();
                foreach (var para in parameters)
                {
                    constCallSite.Append(dicParaStartStr[para.Position]);
                    //处理方法参数是否被Wrapper start
                    string paraTypeName = typeHelper.GetDynamicWrapperName(para.ParameterType);
                    string paraArrayTypeName = typeHelper.GetFristName(para.ParameterType);
                    string paraArrayTypeWrapperName = typeHelper.GetFristWrapperName(para.ParameterType);
                    if (paraTypeName.EndsWithWrapper(const_wrapper))
                    {
                        //数组转换支持
                        if (para.ParameterType.IsArray)
                        {
                            constCallSite.Append("WrapHelper.ArrayConvertFromWrapper<" + paraArrayTypeName + ", " + paraArrayTypeWrapperName + ">(" + para.Name + ")");
                        }
                        else
                        {
                            //constCallSite.Append(para.Name + ".Owner");
                            //输入判空
                            constCallSite.Append("(" + para.Name + " == null ? null : " + para.Name + ".Owner)");
                        }
                    }
                    else
                    {
                        constCallSite.Append(para.Name);
                    }
                    constCallSite.Append(", ");
                }
                string constCallSiteStr = constCallSite.CutLastCommaToString();
                //stream.AppendLine("            this." + wrOwnerName + " = new " + className + "(" + constCallSiteStr + ");");
                //这里给所有构造器都调用Wrapper的构造器实现
                stream.AppendLine("            : this(");
                stream.AppendLine("                UISync.Current.InvokeSync(delegate");
                stream.AppendLine("                {");
                stream.AppendLine("                    return new " + className + "(" + constCallSiteStr + ");");
                stream.AppendLine("                })");
                stream.AppendLine("                ) { }");
                stream.AppendLine("");
            }
            //构造器处理 end

            stream.AppendLine("        #endregion");
            stream.AppendLine("");
        }

        private static void GetFieldsWrapper(Type type, StringBuilder streamAll)
        {
            var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            if (fields.Length > 0)
            {
                streamAll.AppendLine("        #region Fields");
                streamAll.AppendLine("");
                foreach (var field in fields)
                {
                    StringBuilder streamField = new StringBuilder();
                    string fieldName = field.Name;
                    string fieldTypeName = typeHelper.GetDynamicWrapperName(field.FieldType);
                    string fieldArrayTypeName = typeHelper.GetFristName(field.FieldType);
                    string fieldArrayTypeWrapperName = typeHelper.GetFristWrapperName(field.FieldType);
                    string fieldValueType = string.Empty;
                    if (field.FieldType.IsValueType)
                    {
                        fieldValueType = "<" + fieldTypeName + ">";
                    }
                    streamField.AppendLine("        #region " + fieldName);
                    streamField.AppendLine("        public " + fieldTypeName + " " + fieldName);
                    streamField.AppendLine("        {");
                    streamField.AppendLine("            get");
                    streamField.AppendLine("            {");
                    streamField.AppendLine("                return UISync.Current.InvokeSync" + fieldValueType + "(delegate");
                    streamField.AppendLine("                {");
                    if (fieldTypeName.EndsWithWrapper(const_wrapper))
                    {
                        streamField.AppendLine("                    if (Owner." + fieldName + " == null) return null;");
                        //数组转换支持
                        if (field.FieldType.IsArray)
                        {
                            streamField.AppendLine("                    return WrapHelper.ArrayConvertToWrapper<" + fieldArrayTypeWrapperName + ", " + fieldArrayTypeName + ">(Owner." + fieldName + ");");
                        }
                        else
                        {
                            streamField.AppendLine("                    return new " + fieldTypeName + "(Owner." + fieldName + ");");
                        }
                    }
                    else
                    {
                        streamField.AppendLine("                    return Owner." + fieldName + ";");
                    }
                    streamField.AppendLine("                });");
                    streamField.AppendLine("            }");
                    //如果Field是Const或者是ReadOnly,则忽略Set访问器
                    if (!field.IsLiteral && !field.IsInitOnly)
                    {
                        streamField.AppendLine("            set");
                        streamField.AppendLine("            {");
                        streamField.AppendLine("                UISync.Current.InvokeSync(delegate");
                        streamField.AppendLine("                {");
                        if (fieldTypeName.EndsWithWrapper(const_wrapper))
                        {
                            //数组转换支持
                            if (field.FieldType.IsArray)
                            {
                                streamField.AppendLine("                    Owner." + fieldName + " = " + "WrapHelper.ArrayConvertFromWrapper<" + fieldArrayTypeName + ", " + fieldArrayTypeWrapperName + ">(value);");
                            }
                            else
                            {
                                //streamField.AppendLine("                    Owner." + fieldName + " = value.Owner;");
                                //输入判空
                                streamField.AppendLine("                    Owner." + fieldName + " = (value == null ? null : value.Owner);");
                            }
                        }
                        else
                        {
                            streamField.AppendLine("                    Owner." + fieldName + " = value;");
                        }
                        streamField.AppendLine("                });");
                        streamField.AppendLine("            }");
                    }
                    streamField.AppendLine("        }");
                    streamField.AppendLine("        #endregion");
                    streamField.AppendLine("");

                    //静态Filed处理 start
                    if (field.IsStatic)
                    {
                        streamField.Replace("public " + fieldTypeName, "public static " + fieldTypeName);
                        streamField.Replace("Owner.", className + ".");
                    }
                    //静态Filed处理 end
                    streamAll.Append(streamField.ToString());
                }
                streamAll.AppendLine("        #endregion");
                streamAll.AppendLine("");
            }
        }

        private static void GetAllPropertiesWrapper(Type type, StringBuilder streamAll)
        {
            streamAll.AppendLine("        #region Properties");
            streamAll.AppendLine("");
            streamAll.AppendLine("        #region Owner");
            if (baseTypeName.EndsWithWrapper(const_wrapper))
            {
                streamAll.AppendLine("        public new " + className + " Owner");
            }
            else
            {
                streamAll.AppendLine("        public " + className + " Owner");
            }
            streamAll.AppendLine("        {");
            streamAll.AppendLine("            get");
            streamAll.AppendLine("            {");
            streamAll.AppendLine("                return this." + wrOwnerName + ";");
            streamAll.AppendLine("            }");
            streamAll.AppendLine("        }");
            streamAll.AppendLine("        #endregion");
            streamAll.AppendLine("");
            GetPropertiesWrapper(type, streamAll, true);
            GetPropertiesWrapper(type, streamAll, false);
            streamAll.AppendLine("        #endregion");
            //stream.AppendLine("");
        }

        private static void GetPropertiesWrapper(Type type, StringBuilder streamAll, bool isStatic)
        {
            PropertyInfo[] properties = null;
            if (isStatic)
            {
                properties = type.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
            }
            else
            {
                properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            }
            if (properties.Length != 0)
            {
                foreach (PropertyInfo proInfo in properties)
                {
                    StringBuilder streamProperty = new StringBuilder();
                    string proName = proInfo.Name;
                    //索引器处理 start
                    bool isIndex = false;
                    var indexParameters = proInfo.GetIndexParameters();
                    if (indexParameters.Length > 0)
                    {
                        isIndex = true;
                        var streamIndexName = new StringBuilder();
                        var dicIndexStartStr = new Dictionary<int, string>();
                        streamIndexName.Append("this[");
                        streamIndexName.Append(GetParametersString(indexParameters, dicIndexStartStr));
                        streamIndexName.Append("]");
                        proName = streamIndexName.ToString();
                    }
                    string callSiteProName = "Owner." + proName;
                    if (isIndex)
                    {
                        var streamIndexs = new StringBuilder();
                        streamIndexs.Append("Owner[");
                        foreach (var indexParameter in indexParameters)
                        {
                            streamIndexs.Append(indexParameter.Name);
                            if (typeHelper.GetDynamicWrapperName(indexParameter.ParameterType).EndsWithWrapper())
                            {
                                streamIndexs.Append(".Owner");
                            }
                            streamIndexs.Append(", ");
                        }
                        callSiteProName = streamIndexs.CutLastCommaToString() + "]";
                    }
                    //索引器处理 end
                    string proTypeName = typeHelper.GetDynamicWrapperName(proInfo.PropertyType);
                    string proArrayTypeName = typeHelper.GetFristName(proInfo.PropertyType);
                    string proArrayTypeWrapperName = typeHelper.GetFristWrapperName(proInfo.PropertyType);
                    string proValueType = string.Empty;
                    if (proInfo.PropertyType.IsValueType)
                    {
                        proValueType = "<" + proTypeName + ">";
                    }
                    if (proInfo.CanRead || proInfo.CanWrite)
                    {
                        streamProperty.AppendLine("        #region " + proName);
                        streamProperty.AppendLine("        public " + proTypeName + " " + proName);
                        streamProperty.AppendLine("        {");
                    }
                    if (proInfo.GetCanRead())
                    {
                        streamProperty.AppendLine("            get");
                        streamProperty.AppendLine("            {");
                        streamProperty.AppendLine("                return UISync.Current.InvokeSync" + proValueType + "(delegate");
                        streamProperty.AppendLine("                {");
                        if (proTypeName.EndsWithWrapper(const_wrapper))
                        {
                            //属性返回类型需要Wrapper
                            streamProperty.AppendLine("                    if (" + callSiteProName + " == null) return null;");
                            //数组转换支持
                            if (proInfo.PropertyType.IsArray)
                            {
                                streamProperty.AppendLine("                    return WrapHelper.ArrayConvertToWrapper<" + proArrayTypeWrapperName + ", " + proArrayTypeName + ">(" + callSiteProName + ");");
                            }
                            else
                            {
                                streamProperty.AppendLine("                    return new " + proTypeName + "(" + callSiteProName + ");");
                            }
                        }
                        else
                        {
                            streamProperty.AppendLine("                    return " + callSiteProName + ";");
                        }
                        streamProperty.AppendLine("                });");
                        streamProperty.AppendLine("            }");
                    }
                    if (proInfo.GetCanWrite())
                    {
                        streamProperty.AppendLine("            set");
                        streamProperty.AppendLine("            {");
                        streamProperty.AppendLine("                UISync.Current.InvokeSync(delegate");
                        streamProperty.AppendLine("                {");
                        if (proTypeName.EndsWithWrapper(const_wrapper))
                        {
                            //数组转换支持
                            if (proInfo.PropertyType.IsArray)
                            {
                                streamProperty.AppendLine("                    " + callSiteProName + " = " + "WrapHelper.ArrayConvertFromWrapper<" + proArrayTypeName + ", " + proArrayTypeWrapperName + ">(value);");
                            }
                            else
                            {
                                //输入判空
                                streamProperty.AppendLine("                    " + callSiteProName + " = (value == null ? null : value.Owner);");
                            }
                        }
                        else
                        {
                            streamProperty.AppendLine("                    " + callSiteProName + " = value;");
                        }
                        streamProperty.AppendLine("                });");
                        streamProperty.AppendLine("            }");
                    }
                    if (proInfo.CanRead || proInfo.CanWrite)
                    {
                        streamProperty.AppendLine("        }");
                        streamProperty.AppendLine("        #endregion");
                        streamProperty.AppendLine("");
                    }

                    //静态Property处理 start
                    if (isStatic)
                    {
                        streamProperty.Replace("public " + proTypeName, "public static " + proTypeName);
                        streamProperty.Replace("Owner.", className + ".");
                    }
                    //静态Property处理 end
                    streamAll.Append(streamProperty.ToString());
                }
            }
        }

        private static void GetMethodsWrapper(Type type, StringBuilder streamAll)
        {
            MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            if (methods.Length != 0)
            {
                bool hasMethodRegion = false;
                foreach (var method in methods)
                {
                    if ((!method.Name.StartsWith("get_") &&
                        !method.Name.StartsWith("set_")) &&
                        (!method.Name.StartsWith("add_") &&
                        !method.Name.StartsWith("remove_")) &&
                        !method.Name.StartsWith("op_")
                       )
                    {
                        if (!hasMethodRegion)
                        {
                            streamAll.AppendLine("");
                            streamAll.AppendLine("        #region Methods");
                            streamAll.AppendLine("");
                            hasMethodRegion = true;
                        }
                        StringBuilder streamMethod = new StringBuilder();
                        streamMethod.AppendLine("        #region " + method.Name);
                        #region 构造方法返回值信息
                        //构造方法返回值信息 start
                        string retTypeName = string.Empty;
                        string retArrayTypeName = typeHelper.GetFristName(method.ReturnType);
                        string retArrayTypeWrapperName = typeHelper.GetFristWrapperName(method.ReturnType);
                        string retTypeNameG = string.Empty;
                        string returnStr = string.Empty;
                        if (method.ReturnType.Name == "Void")
                        {
                            retTypeName = "void";
                            retTypeNameG = "";
                            returnStr = "";
                        }
                        else if (method.ReturnType.IsValueType)
                        {
                            retTypeName = typeHelper.GetDynamicWrapperName(method.ReturnType);
                            retTypeNameG = "<" + retTypeName + ">";
                            returnStr = "return ";
                        }
                        else
                        {
                            retTypeName = typeHelper.GetDynamicWrapperName(method.ReturnType);
                            retTypeNameG = "";
                            returnStr = "return ";
                        }
                        #endregion//构造方法返回值信息 end
                        #region 构造方法签名
                        //构造方法签名 start  例如:stream.AppendLine("        public void Open(Stream inStream, int sheetIndex, string password)");
                        StringBuilder methodSign = new StringBuilder();
                        methodSign.Append("        ");
                        methodSign.Append("public ");
                        //处理虚函数
                        if (method.IsVirtual)
                        {
                            if (method.Attributes.ToString().IndexOf("VtableLayoutMask") > -1)
                            {
                                methodSign.Append("virtual ");
                            }
                            else
                            {
                                methodSign.Append("override ");
                            }
                        }
                        methodSign.Append(retTypeName);
                        methodSign.Append(" ");
                        //methodSign.Append(method.Name);
                        //Bug Fix 处理泛型方法名
                        string methodName = typeHelper.GetMethodName(method);
                        methodSign.Append(methodName);
                        methodSign.Append("(");
                        #region 构造方法参数信息
                        //构造方法参数信息 start
                        var dicParaStartStr = new Dictionary<int, string>();
                        ParameterInfo[] parameters = method.GetParameters();
                        var parametersStr = GetParametersString(parameters, dicParaStartStr);
                        methodSign.Append(parametersStr);
                        methodSign.Append(")");
                        //泛型方法的约束条件处理
                        methodSign.Append(typeHelper.GetMethodConstraint(method));
                        #endregion//构造方法参数信息 end
                        streamMethod.AppendLine(methodSign.ToString());
                        #endregion//构造方法签名 end
                        streamMethod.AppendLine("        {");
                        //方法调用部分修正 start
                        if (!returnStr.Equals(""))
                        {
                            streamMethod.AppendLine("            " + retTypeName + " result = default(" + retTypeName + ");");
                        }
                        //ref out参数处理开始
                        foreach (var paraOne in parameters)
                        {
                            string refout = dicParaStartStr[paraOne.Position].Trim();
                            if (refout.Equals("ref") || refout.Equals("out"))
                            {
                                string paraOneTypeName = typeHelper.GetName(paraOne.ParameterType);
                                streamMethod.AppendLine("            " + paraOneTypeName + " " + refout + paraOne.Name + " = default(" + paraOneTypeName + ");");
                            }
                        }
                        //ref out参数处理结束
                        //方法调用部分修正 end
                        streamMethod.AppendLine("            UISync.Current.InvokeSync" + retTypeNameG + "(delegate");
                        streamMethod.AppendLine("            {");
                        #region 构造方法调用信息
                        //构造方法调用信息 start  例如:stream.AppendLine("                 this.Owner.Open(inStream, sheetIndex, password);");
                        StringBuilder methodReturnSign = new StringBuilder();
                        methodReturnSign.Append("                ");
                        //方法调用部分修正 start
                        string methodCallStr = GetMethodCallStr(retTypeName, methodName, dicParaStartStr, parameters);
                        if (!returnStr.Equals(""))
                        {
                            streamMethod.AppendLine("                " + typeHelper.GetName(method.ReturnType) + " tempResult = " + methodCallStr);
                            streamMethod.AppendLine("                if (tempResult != null)");
                            methodReturnSign.Append("    result = ");
                        }
                        else
                        {
                            methodReturnSign.Append(returnStr);
                        }
                        //方法调用部分修正 end
                        if (retTypeName.EndsWithWrapper(const_wrapper))
                        {
                            //数组转换支持
                            if (method.ReturnType.IsArray)
                            {
                                methodReturnSign.Append("WrapHelper.ArrayConvertToWrapper<" + retArrayTypeWrapperName + ", " + retArrayTypeName + ">(tempResult);");
                            }
                            else
                            {
                                methodReturnSign.Append("new " + retTypeName + "(tempResult);");
                            }
                            streamMethod.AppendLine(methodReturnSign.ToString());
                        }
                        else
                        {
                            if (returnStr.Equals(""))
                            {
                                streamMethod.AppendLine("                " + methodCallStr);
                            }
                            else
                            {
                                methodReturnSign.Append("tempResult;");
                                streamMethod.AppendLine(methodReturnSign.ToString());
                            }
                        }

                        #endregion//构造方法调用信息 end
                        //方法调用部分修正 start
                        if (!returnStr.Equals(""))
                        {
                            streamMethod.AppendLine("                " + returnStr + "result;");
                        }
                        //方法调用部分修正 end
                        streamMethod.AppendLine("            });");
                        //ref out参数处理开始
                        foreach (var paraReturnOne in parameters)
                        {
                            string refoutreturn = dicParaStartStr[paraReturnOne.Position].Trim();
                            if (refoutreturn.Equals("ref") || refoutreturn.Equals("out"))
                            {
                                string paraReturnOneTypeName = typeHelper.GetDynamicWrapperName(paraReturnOne.ParameterType);
                                if (paraReturnOneTypeName.EndsWithWrapper(const_wrapper))
                                {
                                    streamMethod.AppendLine("            " + paraReturnOne.Name + " = new " + paraReturnOneTypeName + "(" + refoutreturn + paraReturnOne.Name + ");");
                                }
                                else
                                {
                                    streamMethod.AppendLine("            " + paraReturnOne.Name + " = " + refoutreturn + paraReturnOne.Name + ";");
                                }
                            }
                        }
                        //ref out参数处理结束
                        //方法调用部分修正 start
                        if (!returnStr.Equals(""))
                        {
                            streamMethod.AppendLine("            " + returnStr + "result;");
                        }
                        //方法调用部分修正 end
                        streamMethod.AppendLine("        }");
                        streamMethod.AppendLine("        #endregion");
                        streamMethod.AppendLine("");

                        //静态Method处理 start
                        if (method.IsStatic)
                        {
                            streamMethod.Replace("public " + retTypeName, "public static " + retTypeName);
                            streamMethod.Replace("Owner.", className + ".");
                        }
                        //静态Method处理 end
                        streamAll.Append(streamMethod.ToString());
                    }
                }
                if (hasMethodRegion)
                {
                    streamAll.AppendLine("        #endregion");
                    streamAll.AppendLine("");
                }
            }
        }

        private static void GetIWrapperMembers(Type type, StringBuilder stream)
        {
            stream.AppendLine("        #region IWrapper Members");
            stream.AppendLine("");
            stream.AppendLine("        public object GetOwnerInWrapper()");
            stream.AppendLine("        {");
            stream.AppendLine("            return UISync.Current.InvokeSync(delegate");
            stream.AppendLine("            {");
            stream.AppendLine("                return this.Owner;");
            stream.AppendLine("            });");
            stream.AppendLine("        }");
            stream.AppendLine("");
            stream.AppendLine("        public void WrapInitialize(object obj)");
            stream.AppendLine("        {");
            stream.AppendLine("            UISync.Current.InvokeSync(delegate");
            stream.AppendLine("            {");
            stream.AppendLine("                " + wrOwnerName + " = (" + className + ")obj;");
            stream.AppendLine("            });");
            stream.AppendLine("        }");
            stream.AppendLine("");
            stream.AppendLine("        #endregion");
        }

        private static string GetMethodCallStr(string retTypeName, string methodName, Dictionary<int, string> dicParaStartStr, ParameterInfo[] parameters)
        {
            StringBuilder methodCallBuider = new StringBuilder();
            methodCallBuider.Append("Owner.");
            methodCallBuider.Append(methodName);
            methodCallBuider.Append("(");
            foreach (var para in parameters)
            {
                string refoutcall = dicParaStartStr[para.Position].Trim();
                methodCallBuider.Append(dicParaStartStr[para.Position]);
                if (refoutcall.Equals("ref") || refoutcall.Equals("out"))
                {
                    //ref out参数处理
                    methodCallBuider.Append(refoutcall + para.Name);
                }
                else
                {
                    #region 处理方法参数是否被Wrapper
                    //处理方法参数是否被Wrapper start
                    string paraTypeName = typeHelper.GetDynamicWrapperName(para.ParameterType);
                    string paraArrayTypeName = typeHelper.GetFristName(para.ParameterType);
                    string paraArrayTypeWrapperName = typeHelper.GetFristWrapperName(para.ParameterType);
                    if (paraTypeName.EndsWithWrapper(const_wrapper))
                    {
                        //数组转换支持
                        if (para.ParameterType.IsArray)
                        {
                            methodCallBuider.Append("WrapHelper.ArrayConvertFromWrapper<" + paraArrayTypeName + ", " + paraArrayTypeWrapperName + ">(" + para.Name + ")");
                        }
                        else
                        {
                            //methodCallBuider.Append(para.Name + ".Owner");
                            //输入判空
                            methodCallBuider.Append(para.Name + " == null ? null : " + para.Name + ".Owner");
                        }
                    }
                    else
                    {
                        methodCallBuider.Append(para.Name);
                    }
                    //处理方法参数是否被Wrapper end
                }
                    #endregion
                methodCallBuider.Append(", ");
            }
            return methodCallBuider.CutLastCommaToString() + ");";
        }

        private static string GetParametersString(ParameterInfo[] parameters, Dictionary<int, string> dicParaStartStr)
        {
            if (parameters.Length == 0)
            {
                return string.Empty;
            }
            StringBuilder streamParameters = new StringBuilder();
            foreach (var para in parameters)
            {
                var paraAttris = para.GetCustomAttributesData();
                //判断参数类型是否使用Wrapper过的
                string paraTypeName = typeHelper.GetDynamicWrapperName(para.ParameterType);
                #region 构造参数前置out/ref/params信息
                //构造参数前置out/ref/params信息 start
                dicParaStartStr.Add(para.Position, "");
                if (para.IsOut)
                {
                    streamParameters.Append("out ");
                    dicParaStartStr[para.Position] = "out ";
                }
                else if (para.ParameterType.Name.EndsWith("&"))
                {
                    streamParameters.Append("ref ");
                    dicParaStartStr[para.Position] = "ref ";
                }
                else if (para.Position == parameters.Length - 1)
                {
                    foreach (var paraAtt in paraAttris)
                    {
                        if (paraAtt.Constructor.DeclaringType.FullName.Equals("System.ParamArrayAttribute"))
                        {
                            streamParameters.Append("params ");
                            //调用方法时实参不用加params前缀
                            //dicParaStartStr[para.Position] = "params ";
                            break;
                        }
                    }
                }
                #endregion//构造参数前置out/ref/params信息 end
                streamParameters.Append(paraTypeName);
                streamParameters.Append(" ");
                streamParameters.Append(para.Name);
                #region 构造参数默认值
                //构造参数默认值 start
                var hasDefaultValue = false;
                foreach (var paraAtt in paraAttris)
                {
                    if (paraAtt.Constructor.DeclaringType.FullName.Equals("System.Runtime.InteropServices.OptionalAttribute"))
                    {
                        hasDefaultValue = true;
                        break;
                    }
                }
                if (hasDefaultValue)
                {
                    streamParameters.Append(" = ");
                    if (para.DefaultValue == null)
                    {
                        streamParameters.Append("null");
                    }
                    else
                    {
                        if (para.DefaultValue.GetType().Name.Equals("String"))
                        {
                            streamParameters.Append("\"");
                            streamParameters.Append(para.DefaultValue);
                            streamParameters.Append("\"");
                        }
                        else if (para.DefaultValue.GetType().Name.Equals("Char"))
                        {
                            streamParameters.Append("'");
                            streamParameters.Append(para.DefaultValue);
                            streamParameters.Append("'");
                        }
                        else if (para.DefaultValue.GetType().Name.Equals("Boolean"))
                        {
                            if (para.DefaultValue.Equals(true))
                            {
                                streamParameters.Append("true");
                            }
                            else
                            {
                                streamParameters.Append("false");
                            }
                        }
                        else if (para.ParameterType.IsEnum)
                        {
                            streamParameters.Append(Convert.ToInt32(para.DefaultValue).ToString());
                        }
                        else
                        {
                            streamParameters.Append(para.DefaultValue);
                        }
                    }
                }
                #endregion//构造参数默认值 end
                streamParameters.Append(", ");
            }
            return streamParameters.CutLastCommaToString();
        }


        private static bool IsDP(Type type)
        {
            var t = type;
            while (t.BaseType != null)
            {
                if (t.BaseType.Name == "DependencyObject")

                    return true;
                t = t.BaseType;
            }
            return false;
        }

    }

    public class A
    {
        public void fun1()
        {
            
        }

        public int fun2()
        {
            return 0;
        }

        public List<Tuple<int, int, Tuple<string, char, uint>>>  B { get; set; }
    }
}
